//
//  LXTimePickerView.m
//  TestDemo
//
//  Created by xiao.zhao@lachesis-mh.com on 16/1/5.
//  Copyright © 2016年 xiao.zhao@lachesis-mh.com. All rights reserved.
//

#import "LXTimePickerView.h"

@interface LXTimePickerView ()<UIPickerViewDataSource, UIPickerViewDelegate>

@property (nonatomic, strong) UIPickerView *timePickerView;

@property (nonatomic, strong) NSCalendar *calendar;

@property (nonatomic, strong) NSArray *weekArray;
@property (nonatomic, strong) NSMutableArray *dayList;

@property (nonatomic, assign) NSInteger todayIndex;

@end

@implementation LXTimePickerView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        [self setup];
    }
    return self;
}

-(void)setup
{
    self.weekArray = @[@"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六"];
}

-(void)selectWithComponents:(NSDateComponents *)dateComponents
{
    if (!_timePickerView) {
        _timePickerView = [[UIPickerView alloc]initWithFrame:self.bounds];
        _timePickerView.dataSource = self;
        _timePickerView.delegate = self;
        _timePickerView.showsSelectionIndicator = YES;
        [self addSubview:_timePickerView];
    }
    [_timePickerView selectRow:_todayIndex inComponent:0 animated:YES];
    [_timePickerView selectRow:dateComponents.hour inComponent:1 animated:YES];
    [_timePickerView selectRow:dateComponents.minute inComponent:2 animated:YES];
}

-(NSDate *)selectedDate
{
    NSInteger selComponent1 = [_timePickerView selectedRowInComponent:0];
    NSInteger selComponent2 = [_timePickerView selectedRowInComponent:1];
    NSInteger selComponent3 = [_timePickerView selectedRowInComponent:2];
    
    NSString *dayStr = _dayList[selComponent1];
    NSArray *dayArray = [dayStr componentsSeparatedByString:@" "];
    
    NSString *dateStr = [NSString stringWithFormat:@"%@ %02ld:%02ld", dayArray[1], selComponent2, selComponent3];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm";
    
    return [dateFormatter dateFromString:dateStr];
}

- (void)setSelectedDate:(NSDate *)selectedDate
{
    _dayList = [[NSMutableArray alloc]init];
    self.calendar = [NSCalendar currentCalendar];
    _calendar.timeZone = [NSTimeZone localTimeZone];
    _calendar.firstWeekday = 1;
    
    NSDateComponents *nowComponents = [_calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute fromDate:selectedDate];
    
    NSDateComponents *dateComponents = [[NSDateComponents alloc]init];
    dateComponents.year = nowComponents.year;
    
    for (int i = 0; i < 12; i++) {
        dateComponents.month = i + 1;
        
        NSDate *date_month = [_calendar dateFromComponents:dateComponents];
        //这个月有多少天
        NSRange dayRange = [_calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitYear forDate:date_month];
        
        for (int j = 0; j < dayRange.length; j++) {
            dateComponents.day = j + 1;
            NSDate *date_day = [_calendar dateFromComponents:dateComponents];
            
            NSDateComponents *components = [_calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday fromDate:date_day];
            
            NSString *dayStr = [NSString stringWithFormat:@"%@ %ld-%02ld-%02ld", _weekArray[components.weekday - 1], components.year, components.month, components.day];
            
            if((i + 1) == nowComponents.month &&
               (j + 1) == nowComponents.day)
            {
                self.todayIndex = _dayList.count;
            }
            
            [_dayList addObject:dayStr];
        }
    }
    
    [self selectWithComponents:nowComponents];
}

#pragma mark -UIPickerViewDataSource
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    NSInteger rowCount = 0;
    switch (component) {
        case 0:
        {
            rowCount = _dayList.count;
        }
            break;
        case 1:
        {
            rowCount = 24;
        }
            break;
        case 2:
        {
            rowCount = 60;
        }
            break;
        default:
            break;
    }
    
    return rowCount;
}

-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    
    CGFloat width = 0;
    switch (component) {
        case 0:
        {
            width = 220.0f / 375.0f * screenWidth;
        }
            break;
        case 1:
        {
            width = 50.0f / 375.0f * screenWidth;
        }
            break;
        case 2:
        {
            width = 50.0f / 375.0f * screenWidth;
        }
            break;
        default:
            break;
    }
    
    return width;
}

-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 44.0f;
}

-(NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString *title = nil;
    switch (component) {
        case 0:
        {
            title = _dayList[row];
        }
            break;
        case 1:
        {
            title = [NSString stringWithFormat:@"%02ld", row];
        }
            break;
        case 2:
        {
            title = [NSString stringWithFormat:@"%02ld", row];
        }
            break;
        default:
            break;
    }
    
    NSAttributedString *attrStr = [[NSAttributedString alloc]initWithString:title attributes:@{NSForegroundColorAttributeName : kAppThemeColor, NSFontAttributeName : [UIFont lx_systemFontOfSize:18.0f]}];
    
    return attrStr;
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if(component == 0 && row > _todayIndex)
    {
        [pickerView selectRow:_todayIndex inComponent:0 animated:YES];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
